package com.foundation.java;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import etl.db.TestOracle;

public class Test {
	
	private static String querySql = "SELECT COUNT(*) FROM T_ZDRYBH_MAJOR";
	
	public void testAdd() {
		int i = -1;
		int len = 3;
		//i先自加1，然后再和len比较
		while(++i<len) {
			System.out.println("i="+i);
		}
	}
	
	public String testDivide() throws Exception {
		String flag = "Y";
		try {
			int i = 0;
			int j = 9/i;
		} catch (Exception e) {
			flag = "N";
			System.out.println("testDivide--flag="+flag);
//			e.printStackTrace();
			throw e;
		} finally {
			return flag;
		}
//		return flag;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
/*		
		new Test().testAdd();
		
		List<String> list = new ArrayList<String>();
		
		for(int i=0; i<3 ;i++) {
			String obj = new String("taowei");
			list.add(obj);
		}
		
		for (String str: list) {
			System.out.println(str);
		}*/
		String isSu = "";
		try {
			isSu = new Test().testDivide();
			System.out.println("in--isSu="+isSu);
		} catch (Exception e) {
			System.out.println("catch--isSu="+isSu);
//			e.printStackTrace();
		}
		System.out.println("out--isSu="+isSu);
		
		Connection connection = null;
		PreparedStatement pstmt = null;
		
		TestOracle to = new TestOracle();
		connection = to.connectDbByThin();
		ResultSet rSet = null;
		try {
			pstmt = connection.prepareStatement(querySql);
//			pstmt.setQueryTimeout(2);
			rSet = pstmt.executeQuery();
			while(rSet.next()) {
				Integer count = rSet.getInt(1);
				System.out.println("count="+count);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	public static Object exeTask(Callable callable) throws InterruptedException, SQLException, TimeoutException, ExecutionException{
		Object o = null;
		FutureTask task = new FutureTask(callable);
		Thread t = new Thread(task);
		t.start();
		try {
			 o = task.get(1, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw e;
		} catch (ExecutionException e) {
			if(e.getCause() instanceof SQLException){
				SQLException sqlException = (SQLException) e.getCause();
				throw sqlException;
			}
			throw e;
		} catch (TimeoutException e) {
			e.printStackTrace();
			SQLException sqlException = new SQLException("执行数据库操作超时", "-1");
			throw sqlException;
		}
		return o;
	}

}
